//
//  ViewController.m
//  cell 单选
//
//  Created by MB__Lee on 2018/4/16.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "MBCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property(nonatomic,strong)NSMutableArray *mut;


@property(nonatomic,strong)MBCell *nowSelectedCell;

@property(nonatomic,strong)NSIndexPath *selectedIndexPath;
@end

@implementation ViewController

static NSString *identi = @"MBCell";

-(NSMutableArray *)mut{
    if (!_mut) {
        _mut  = [NSMutableArray array];
        
        [_mut addObjectsFromArray:@[@"1",@"2",@"3",@"4",@"5",@"6",@"1",@"2",@"3",@"4",@"5",@"6",@"1",@"2",@"3",@"4",@"5",@"6"]];
    }
    return  _mut;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self.tableview registerNib:[UINib nibWithNibName:identi bundle:nil] forCellReuseIdentifier:identi];
    
    self.tableview.rowHeight = 150;
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.mut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MBCell *cell = [tableView dequeueReusableCellWithIdentifier:identi];
    if (indexPath == _selectedIndexPath) {
        [cell MBsetSelected];
    }else{
        [cell MBSetNoSelected];
    }
    
    return  cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [_nowSelectedCell MBSetNoSelected];
    
    
    MBCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell MBsetSelected];
    
    
    _nowSelectedCell = cell;//记录选中cell
    
    _selectedIndexPath = indexPath;//记录选中的indexPath
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
